local addonName, IWonew = ...

AuctionHouseClass = {}
AuctionHouseClass.__index = AuctionHouseClass

function AuctionHouseClass:new()
  local instance = setmetatable({}, AuctionHouseClass)
  instance.id = 666129
	return instance
end

function AuctionHouseClass:registerEvents()
  local f = CreateFrame("Frame")
  f:RegisterEvent("ITEM_SEARCH_RESULTS_UPDATED")
  f:RegisterEvent("COMMODITY_SEARCH_RESULTS_UPDATED")
  local rootSelf = self
  function f:OnEvent(event, ...)
    rootSelf[event](rootSelf, event, ...)
  end
  f:SetScript("OnEvent", f.OnEvent)
end

function AuctionHouseClass:ComputeProfit()
  for i = 1, #TargetEnchantingItems do
    local curPrice = IWonew.getListItem(self.ScanEnchantingProfit_items, "itemID", TargetEnchantingItems[i].itemID).unitPrice
    local regantPrice = 0
    for j = 1, #TargetEnchantingItems[i].regants do
      local quality = TargetEnchantingItems[i].regants[j].quality
      local unitPrice = IWonew.getListItem(self.ScanEnchantingProfit_items, "itemID", TargetEnchantingItems[i].regants[j].itemID).unitPrice
      regantPrice = regantPrice + quality * unitPrice
    end
    self:AddRegentProfitToLog()
    print(TargetEnchantingItems[i].name .. "价格:" .. curPrice / 10000 .. "材料价格：" .. regantPrice / 10000 .. "利润：" .. (curPrice - regantPrice) / 10000)
  end
end

function AuctionHouseClass:AddRegentProfitToLog()
  local logString = "itemID_100=100"
  for i = 1, #self.ScanEnchantingProfit_items do
    logString = logString .. "&itemID_" .. self.ScanEnchantingProfit_items[i].itemID .. "=" .. self.ScanEnchantingProfit_items[i].unitPrice
    -- {name = TargetEnchantingItems[i].name, itemID = TargetEnchantingItems[i].itemID, unitPrice = nil}
  end
  IWonew.Log("CRAFT_REGENTS_UNIT_PRICE_UPDATE----" .. "{" .. logString .. "}")
end

function AuctionHouseClass:COMMODITY_SEARCH_RESULTS_UPDATED(event, itemID)
  local averagePrice = 0
  for i = 1, 3 do
    local result = C_AuctionHouse.GetCommoditySearchResultInfo(itemID, i)
    local unitPrice = result and result.unitPrice or 0
    averagePrice = averagePrice + unitPrice
  end
  averagePrice = math.ceil(averagePrice / 3)
  local cachedItem = IWonew.getListItem(self.ScanEnchantingProfit_items, "itemID", itemID)
  if cachedItem and cachedItem.unitPrice == nil then
    cachedItem.unitPrice = averagePrice
    print("扫描结果", averagePrice)
    local unQueryItems = IWonew.filterTable(self.ScanEnchantingProfit_items, function(item)
      return item.unitPrice == nil
    end)
    if #self.ScanEnchantingProfit_items > 0 and #unQueryItems <= 0 then
      print("扫描完毕，开始计算利润")
      self:ComputeProfit()
    end
  end
  -- C_AuctionHouse.PostItem(ItemLocation:CreateFromBagAndSlot(0, 1), 3, 1, nil, 4200)
end

-- function AuctionHouseClass:Query()
-- end

function AuctionHouseClass:ScanEnchantingProfit_SetRegantItems()
  self.ScanEnchantingProfit_items = {}
  if #self.ScanEnchantingProfit_items > 0 then return end
  for i = 1, #TargetEnchantingItems do
    table.insert(self.ScanEnchantingProfit_items, {name = TargetEnchantingItems[i].name, itemID = TargetEnchantingItems[i].itemID, unitPrice = nil})
    for j = 1, #TargetEnchantingItems[i].regants do
      if not IWonew.getListItem(self.ScanEnchantingProfit_items, "itemID", TargetEnchantingItems[i].regants[j].itemID) then
        table.insert(self.ScanEnchantingProfit_items, {name = TargetEnchantingItems[i].regants[j].name, itemID = TargetEnchantingItems[i].regants[j].itemID})
      end
    end
  end
end

function AuctionHouseClass:ScanEnchantingProfit()
  self:ScanEnchantingProfit_SetRegantItems()
  local sorts = {
    {sortOrder = Enum.AuctionHouseSortOrder.Price, reverseSort = false},
  }
  local lastQueryIdx = 0
  local ticker
  ticker = C_Timer.NewTicker(1, function()
    lastQueryIdx = lastQueryIdx + 1
    if lastQueryIdx > #self.ScanEnchantingProfit_items then
      ticker:Cancel()
      return
    end
    local itemKey = {itemID = self.ScanEnchantingProfit_items[lastQueryIdx].itemID}
    print("开始扫描", self.ScanEnchantingProfit_items[lastQueryIdx].name)
    C_AuctionHouse.SendSearchQuery(itemKey, sorts, false)
  end)
end

local AuctionHouse_instance = nil

function CreateAuctionHouseInstance()
  if AuctionHouse_instance == nil then
    AuctionHouse_instance = AuctionHouseClass:new()
    AuctionHouse_instance:registerEvents()
  end
  return AuctionHouse_instance
end

function ScanEnchantingProfit()
  local instance = CreateAuctionHouseInstance()
  instance:ScanEnchantingProfit()
end


